package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.DraRecordMapper;
import com.wn.mapper.DrugMapper;
import com.wn.mapper.UserMapper;
import com.wn.ov.DraRecordOv;
import com.wn.ov.DrugRecordOv;
import com.wn.ov.UserOv;
import com.wn.pojo.DraRecord;
import com.wn.common.PageParam;
import com.wn.pojo.Drug;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DraRecordService {

    @Autowired
    private DraRecordMapper draRecordMapper;

    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private UserMapper userMapper;


    //退药申请记录
    public ServerResponse addDraRecord(DraRecordOv draRecordOv, UserOv userOv){
        DraRecord draRecord =new DraRecord();
        //获取数量和单价
        Integer number = draRecordOv.getNumber();
        
        //通过药品的的名称去查找药品的信息
        Drug drug = drugMapper.getDrugByName(draRecordOv.getName());

        BigDecimal price = drug.getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(number));

        //通过病人的姓名来查找病人的id
        User patient = userMapper.getPatientByName(draRecordOv.getRealName());
        if(StringUtils.isEmpty(patient)){
            return new ServerResponse(ResponseCode.ERROR,"病人姓名输入错误！");
        }
        //添加小计金额
        draRecord.setSubtotal(multiply);
        draRecord.setDrugId(drug.getId());
        draRecord.setNumber(number);
        draRecord.setPatientId(patient.getId());
        draRecord.setCreateUserId(userOv.getId());
        draRecord.setPrice(price);
        //调用添加方法
        int insert = draRecordMapper.insertSelective(draRecord);
        if(insert!=0){
            return new ServerResponse(ResponseCode.SUCCESS,"添加成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"添加失败");
        }
    }

    /**
     * 确认退药
     */
    public ServerResponse confirmDraRecord(DraRecord draRecord){
        int update = draRecordMapper.updateStatus(draRecord);
        if(update!=0){
            return new ServerResponse(ResponseCode.SUCCESS,"修改成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"修改失败");
        }
    }

    /**
     * 取消退药
     */
    public ServerResponse cancelDraRecord(DraRecord draRecord){
        int update = draRecordMapper.updateStatus2(draRecord);
        if(update!=0){
            return new ServerResponse(ResponseCode.SUCCESS,"修改成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"修改失败");
        }
    }

    /**
     * 删除记录
     */
    public ServerResponse deleteDraRecord(DraRecord draRecord){
        int delete = draRecordMapper.deleteByPrimaryKey(draRecord.getId());
        if(delete!=0){
            return new ServerResponse(ResponseCode.SUCCESS,"删除成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"删除失败");
        }
    }

    //查看退药记录
    public ServerResponse getAllDraRecord(PageParam<DraRecord> pageParam){
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<DraRecordOv> draRecordOvList = draRecordMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<DraRecordOv> draRecordOvPageInfo = new PageInfo<DraRecordOv>(draRecordOvList);
            return new ServerResponse(ResponseCode.SUCCESS,draRecordOvPageInfo);
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<DraRecordOv> list = draRecordMapper.selectAll();
            PageInfo<DraRecordOv> pageInfo = new PageInfo<DraRecordOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS,pageInfo);
        }
    }

    //查询药品列表
    public ServerResponse getDrugList(){
        List<Drug> drugList = drugMapper.getDrugList();
        return new ServerResponse(ResponseCode.SUCCESS,drugList);
    }

}
